﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System.Configuration;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Unity.Configuration;

#endregion

namespace Toulr.Util.Ioc
{
    public class UnityHelper 
    {
        private static IUnityContainer _container;

        #region IocHelper<TEntity> Members
     
        public static T GetInstance<T>()
        {
            if (_container == null) InitContainer();
            return _container.Resolve<T>();
        }

        #endregion

        private static void InitContainer()
        {
            _container = new UnityContainer();
            // 从配置文件读取unity节点的配置
            var section = (UnityConfigurationSection) ConfigurationManager.GetSection("unity");
            // 将读取的配置信息应用到Unity 容器
            section.Containers.Default.Configure(_container);
        }
    }
}